package com.czl.module_service.fragment.assetHandle

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.AssetHandleSortBean
import com.czl.base.event.LiveBusCenter
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.AssetHandleProductInuseAdapter
import com.czl.module_service.adapter.AssetHandleSortInuseAdapter
import com.czl.module_service.databinding.FragmentAssetHandleSortInuseBinding
import com.czl.module_service.viewmodel.AssetHandleSortInuseViewModel

@Route(path = AppConstants.Router.Service.F_SERVICE_ASSET_HANDLE_SORT_INUSE)
class AssetHandleSortInuseFragment : BaseFragment<FragmentAssetHandleSortInuseBinding, AssetHandleSortInuseViewModel>() {

    private lateinit var sAdapter: AssetHandleSortInuseAdapter
    private lateinit var pAdapter: AssetHandleProductInuseAdapter

    override fun initContentView(): Int {
        return R.layout.fragment_asset_handle_sort_inuse
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        super.initData()
        initAdapter()
        reload()
    }

    override fun useBaseLayout(): Boolean {
        return false
    }

    private fun initAdapter() {
        sAdapter = AssetHandleSortInuseAdapter().apply {
        }
        pAdapter = AssetHandleProductInuseAdapter().apply {
        }
        binding.ryCommon.apply{
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = sAdapter
        }
        sAdapter.productIdList =
            arguments?.getIntegerArrayList("productIdList") as ArrayList<Int>
        viewModel.parentCompanyId = arguments?.getSerializable("parentCompanyId") as Int
        LiveBusCenter.observeAddAssetHanldeSortSelectEvent(this, {
            viewModel.sortIdInfoList = it.data as HashMap<String, Int>
        })
    }

    override fun initViewObservable() {
//        viewModel.uc.loadCompleteEvent.observe(this, { data ->
//            this.loadCompletedData(
//                data?.hasNextPage,
//                viewModel.currentPage == 2,
//                data != null,
//                data?.total,
//                data?.list,
//                mAdapter,
//                binding.ryCommon,
//                binding.smartCommon
//            )
//        })
        viewModel.uc.loadSortEvent.observe(this,{
                data->
//            binding.smartCommon.finishRefresh(true)
            sAdapter.setNewInstance(data as MutableList<AssetHandleSortBean>?)
        })
        viewModel.uc.loadProductEvent.observe(this,{
                data->
//            binding.smartCommon.finishRefresh(true)
            pAdapter.setNewInstance(data as MutableList<AssetHandleSortBean>?)
        })
        viewModel.uc.loadSubmitEvent.observe(this, {data ->
            LiveBusCenter.postAddAssetHanldeSortEvent(data,"inuse",0)
        })
    }

    override fun reload() {
        viewModel.getProcessSortUseInfo()
//        viewModel.getProcessProUseInfo()
//        binding.smartCommon.autoRefresh()
    }
}